package com.elasticsearch.demo.controller;

import com.elasticsearch.demo.entity.ManEntity;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

@RestController
public class EsController {

    @Autowired
    private TransportClient client;

    @GetMapping("get/people/man")
    public ResponseEntity get(@RequestParam(name = "id") String id) {
        GetResponse result = this.client.prepareGet("people", "man", id).get();

        if (!result.isExists()) {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }

        return new ResponseEntity(result.getSource(), HttpStatus.OK);
    }

    @PostMapping("add/people/man")
    public ResponseEntity add(@RequestBody ManEntity entity) throws IOException {
        System.out.println("入参:" + entity);
        XContentBuilder content = XContentFactory.jsonBuilder().startObject()
                .field("name", entity.getName())
                .field("country", entity.getCountry())
                .field("date", entity.getDate())
                .field("age", entity.getAge())
                .endObject();
        IndexResponse result = this.client.prepareIndex("people", "man").setSource(content).get();

        return new ResponseEntity(result.getId(), HttpStatus.OK);
    }

    @DeleteMapping("delete/people/man")
    public ResponseEntity delete(@RequestParam(name = "id") String id){
        DeleteResponse result = this.client.prepareDelete("people", "man", id).get();
        return new ResponseEntity(result.getResult().toString(), HttpStatus.OK);
    }

    @PutMapping("update/people/man")
    public ResponseEntity update(@RequestBody ManEntity entity) throws IOException, ExecutionException, InterruptedException {
        System.out.println("入参:" + entity);
        UpdateRequest update = new UpdateRequest("people", "man", entity.getId());

        XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
                .field("name", entity.getName())
                .field("country", entity.getCountry())
                .endObject();
        update.doc(builder);
        UpdateResponse result = this.client.update(update).get();
        return new ResponseEntity(result.getResult().toString(), HttpStatus.OK);
    }

    @PostMapping("query/people/man")
    public ResponseEntity query(@RequestParam(name = "name") String name) {
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        boolQuery.must(QueryBuilders.matchQuery("name", name));

        SearchRequestBuilder builder = this.client.prepareSearch("people").setTypes("man").setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
                .setFrom(0).setSize(10);
        System.out.println("查询返回:" + builder);

        SearchResponse response = builder.get();
        List<Map<String, Object>> result = new ArrayList();
        for (SearchHit hit : response.getHits()) {
            result.add(hit.getSourceAsMap());
        }
        return new ResponseEntity(result, HttpStatus.OK);
    }

}
